/**
 * author: Hanflyee  flyee1980@163.com
 * vision: v1.0
 * data: 2009-7-9
 */
package com.flyee.controller{
	import com.flyee.model.Component;
	import com.flyee.service.pool.ObjectCollection;
	import com.flyee.service.pool.RelationMV;
	import com.flyee.utils.Obsevable;
	import com.flyee.utils.Obsever;
	import com.flyee.utils.load.StackLoader;
	import com.flyee.utils.load.model.LoaderModel;
	import com.flyee.utils.load.processor.LoadLevelType;
	import com.flyee.utils.load.processor.LoadReturnType;
	import com.flyee.viewer.ComponentViewer;
	
	/**
	 * 组件控制器 控制组件行为 ：加载，隐藏，点击事件 
	 */
	public class ComponentController extends Obsevable implements Obsever{
		
		public function ComponentController(){
			
		}
		
		/**
		 * 被观察者通知观察者执行 
		 */
		public function execute(o : Obsevable, command : String=null, args : * = null) : void {
			switch(command){
				case "unloadComponent" : 
					unloadComponent(args[0]);
					break ;
				default :
//					MyLog.debug("command is null!");
					break ;
			}
		}
		
		/**
		 * 加载组件 
		 */
		public function loadComponent(comp : Component) : void {			
			var compUI : ComponentViewer = new ComponentViewer(this);
			ObjectCollection.componentViewerMap.put(compUI.vid, compUI);
			ObjectCollection.relationArray.push(new RelationMV(comp.mid, compUI.vid));
			var loader : StackLoader = new StackLoader();
			var memo : Object = new Object();
			memo.vid = compUI.vid;
			memo.layer = comp.layer;
			memo.x = comp.x;
			memo.y = comp.y;
			loader.load(comp.url, LoadLevelType.LV_2, null, loadComponentHandle, LoadReturnType.RETURN_LOADER, memo);	
			//通知观察者执行
			setChanged();
			notifyObsevers("addComponent", compUI, memo.layer, memo.x, memo.y);		
		}
		
		public function loadComponentHandle(loader : LoaderModel) : void {
			var compUI : ComponentViewer = ObjectCollection.componentViewerMap.get(loader.memo.vid) as ComponentViewer;
			compUI.addChild(loader);
			
		}
		
		public function unloadComponent(componentUI : ComponentViewer) : void {
			componentUI.dispose();
		}

	}
	
}